<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * Copyright (C) 2007  Robert Moss
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
  /**
   * print edit row for game
   *
   * @param object $match
   * @param array objects $resultTeams
   * @param int $i row number
   * @param array $config
   */
  function editable_row (&$match, &$resultTeams, $i, &$config) {
    global $database, $my, $joomleague;
  
    $thismatch = new Match ($database);
    $thismatch->bind(get_object_vars($match));
        
    list($datum, $uhrzeit)=split(" ", $thismatch->match_date);
    
    $team1 = $resultTeams[$thismatch->matchpart1];
    $team2 = $resultTeams[$thismatch->matchpart2];
    
    $userIsTeamAdmin = ( $my->id == $team1->admin || $my->id == $team2->admin );
    
    $teams = $resultTeams;
    foreach ($teams AS $team) {
      $teamsoptions[] = mosHTML::makeOption($team->id, $team->name);
    }
    ?>
      <tr class="<?php echo $config['class'];?>">
        <td nowrap="nowrap" valign="top"><?php
        if ($thismatch->checked_out && $thismatch->checked_out != $my->id) {
          $query="SELECT username FROM #__users WHERE id = $match->checked_out";
          $joomleague->_db->setQuery($query);
          $username = $joomleague->_db->loadResult();
          echo '<acronym title="CHECKED OUT BY '.$username.'">x</acronym>';
        }
        else { ?> <input type="checkbox" id="cb<?php echo $i;?>"
          name="msid[]" value="<?php echo $thismatch->match_id; ?>" /> <a
          href="javascript:void(0)"
          onclick="loadmatchdetails(<?php
            echo $thismatch->match_id.", ".$i;?>);return false;"> <img
          src="<?php echo JL_TEMPLATE_FOLDERS_URL.'/images/edit.png" alt="'
            ._JL_EDIT_DETAILS.'" title="'._JL_EDIT_DETAILS.'" border="0" id="edit'.$thismatch->match_id;?>" /></a>
            <?php } ?></td>
            <?php
            if ($config['show_match_number']==1) {
               ?>
        <td valign="top"><input type="text" class="inputbox" size="2"
          name="match_number<?php echo $thismatch->match_id;?>"
          value="<?php
            echo $thismatch->match_number;?>"
          onchange="$('cb<?php echo $i;?>').checked=true;" /></td>
          <?php } ?>
        <td nowrap="nowrap" valign="top"><input type="text" size="8"
          name="match_date<?php echo $thismatch->match_id;?>"
          id="match_date<?php
            echo $thismatch->match_id;?>"
          value="<?php echo $datum;?>" class="inputbox"
          onchange="$('cb<?php
            echo $i;?>').checked=true;" /> <input type="reset" class="inputbox"
          value="..."
          onclick="return showCalendar('match_date<?php
            echo $thismatch->match_id;?>', 'y-mm-dd');"
          onchange="$('cb<?php echo $i;?>').checked=true;" />
  <?php
  if (isset($config['results_below']) && $config['results_below']==1) {?>     <br /><?php }
  else { ?>
    </td>
        <td align="center" nowrap="nowrap" valign="top">
  <?php } ?>
        <input type="text" size="6"
          name="match_time<?php echo $thismatch->match_id;?>"
          value="<?php
            echo $uhrzeit;?>" class="inputbox"
          onchange="$('cb<?php echo $i;?>').checked=true;" /> <?php
          if (isset($config['results_below']) && $config['results_below']==1) echo _JL_CLOCK;?>
        </td>
        <td nowrap="nowrap" valign="top"><?php
        $append='class="inputbox" size="1" onchange="$(\'cb'.$i.'\').checked=true;"';
        if (!(defined('JL_USER_IS_PROJECT_ADMIN') || defined('JL_USER_IS_PROJECT_EDITOR')
        || defined('JL_USER_IS_BIG_BOSS')) && $userIsTeamAdmin ) {
          $append .= ' disabled="disabled"';
        }
        echo mosHTML::selectList( $teamsoptions, 'matchpart1'.$thismatch->match_id, $append,'value', 'text', $thismatch->matchpart1 );
        if (isset($config['results_below']) && $config['results_below']==1) {?>     <br /><?php }
         else { ?>
          </td>
        <td width="5" valign="top">-</td>
        <td nowrap="nowrap" align="left" valign="top">
          <?php
          }
        echo mosHTML::selectList( $teamsoptions, 'matchpart2'.$thismatch->match_id, $append,'value', 'text', $thismatch->matchpart2 );
      ?></td>
    <?php
    if ( isset($config['results_below']) && $config['results_below']==1 ) 
    {
      $partresults1=explode(";", $thismatch->matchpart1_result_split);
      $partresults2=explode(";", $thismatch->matchpart2_result_split);
       
      for ( $x=0;$x<($joomleague->game_parts);$x++ ) 
      {
        ?>
        <td align="right">
          <input type="text" style="font-size:9px;" 
                 name="matchpart1_result_split<?php echo $thismatch->match_id;?>[]" 
                 value="<?php echo (isset($partresults1[$x])) ? $partresults1[$x] : '';?>" 
                 size="4" tabindex="1" class="inputbox" 
                 onchange="$('cb<?php echo $i;?>').checked=true;update_points( 1, <?php echo $thismatch->match_id;?> );"/>
          <br />
          <input type="text" style="font-size:9px;" 
                 name="matchpart2_result_split<?php echo $thismatch->match_id;?>[]" 
                 value="<?php echo (isset($partresults2[$x])) ? $partresults2[$x] : '';?>" 
                 size="2" tabindex="1" class="inputbox" 
                 onchange="$('cb<?php echo $i;?>').checked=true;update_points( 2, <?php echo $thismatch->match_id;?> );" />
        </td>  
        <?php 
      }
        if ($joomleague->allow_add_time == 1) {
  ?>
           <td valign="top" align="right">
           <span id="ot<?php echo $thismatch->match_id;?>" style="visibility:<?php echo ($thismatch->match_result_type > 0) ? 'visible' : 'hidden';?>">
           <input type="text" style="font-size:9px;" name="matchpart1_result_ot<?php
            echo $thismatch->match_id;?>" value="<?php
            echo (isset($thismatch->matchpart1_result_ot)) ? $thismatch->matchpart1_result_ot : '';?>" size="2" tabindex="1" class="inputbox"
             onchange="$('cb<?php
            echo $i;?>').checked=true;" /><br /><input type="text" style="font-size:9px;" name="matchpart1_result_ot<?php
            echo $thismatch->match_id;?>" value="<?php
            echo (isset($thismatch->matchpart2_result_ot)) ? $thismatch->matchpart2_result_ot : '';?>" size="2" tabindex="1" class="inputbox"
             onchange="$('cb<?php
            echo $i;?>').checked=true;" /></span></td>
        <?php }
        ?>
        <td nowrap="nowrap" valign="top" align="right"><input type="text"
          name="matchpart1_result<?php echo $thismatch->match_id;?>"
          value="<?php echo $thismatch->matchpart1_result;?>" size="2"
          tabindex="1" class="inputbox"
          onchange="$('cb<?php echo $i;?>').checked=true;" /><br /><input type="text"
          name="matchpart2_result<?php echo $thismatch->match_id;?>"
          value="<?php echo $thismatch->matchpart2_result;?>" size="2"
          tabindex="1" class="inputbox"
          onchange="$('cb<?php echo $i;?>').checked=true;" />
        </td>
  <?php
    if ($joomleague->use_legs==1) {
    ?><td valign="top" align="right">
      <input type="text" style="font-size:9px;" name="matchpart1_legs<?php echo $thismatch->match_id;?>" value="<?php echo $thismatch->matchpart1_legs;
      ?>" size="2" tabindex="1" class="inputbox" onchange="$('cb<?php echo $i;?>').checked=true;" />
        <br />
        <input type="text" style="font-size:9px;" name="matchpart2_legs<?php echo $thismatch->match_id;?>" value="<?php echo $thismatch->matchpart2_legs;
        ?>" size="2" tabindex="1" class="inputbox" /></td>
  <?php }
    } 
    
  
  else { ?>
    <td nowrap="nowrap" align="center" valign="top">&nbsp;&nbsp;&nbsp;<input type="text"
          name="matchpart1_result<?php echo $thismatch->match_id;?>"
          value="<?php echo $thismatch->matchpart1_result;?>" size="3"
          tabindex="1" class="inputbox"
          onchange="$('cb<?php echo $i;?>').checked=true;" />
        : <input type="text"
          name="matchpart2_result<?php echo $thismatch->match_id;?>"
          value="<?php echo $thismatch->matchpart2_result;?>" size="3"
          tabindex="1" class="inputbox"
          onchange="$('cb<?php echo $i;?>').checked=true;" />
          <?php
          $thismatch->editPart_results ($i, $joomleague);?>
    </td>
  <?php }
    if($joomleague->allow_add_time){
      $xrounds = array();
      $xrounds[] = mosHTML::makeOption(0, _JL_RESULTS_REGULAR);
      $xrounds[] = mosHTML::makeOption(1, _JL_RESULTS_OVERTIME);
      $xrounds[] = mosHTML::makeOption(2, _JL_RESULTS_PENALTY); 
      echo '<td valign="top">'.mosHTML::selectList( $xrounds, "match_result_type$thismatch->match_id", 'class="inputbox" size="1" 
      onchange="if (this.selectedIndex==0) $(\'ot'.$thismatch->match_id.'\').style.visibility=\'hidden\';else $(\'ot'.$thismatch->match_id.'\').style.visibility=\'visible\';"','value', 'text', $thismatch->match_result_type ).'</td>';
    } 
    echo '<td colspan="'.$config['second_colspan'].'">&nbsp;</td>';
  ?>
      </tr>
        <?php
  }

global $mainframe;

?>

<a name="jl_top" id="jl_top"></a>
<!-- Header with Project Name-->
<?php 
if ($results_config['show_project_heading'] > 0 OR $overall_config['show_project_heading'] > 0) 
{ ?>
  <div class="componentheading" <?php if ($results_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") echo 'style="float:left;"';?>><?php echo HTML_joomleague::showTitle($joomleague, $overall_config['project_heading_type']); ?></div>
  <!-- Druckfunktion -->
  <?php 
  if ($results_config['show_print_button'] == "1" OR $overall_config['show_print_button'] == "1") 
  {
    HTML_joomleague::PrintIcon( $row, $params, false, '' );
  }
  ?>
  <!-- Druckfunktion Ende -->
  <?php 
} 
?>
<!-- Header END -->

<!-- section header e.g. ranking, results etc. -->

<?php 
// load javascripts
$mainframe->addCustomHeadTag( '
	<script src="'. JL_URL_FOLDER.'/js/JL_eventsediting.js" type="text/javascript"></script>
	<!-- import the calendar script -->
  <script language="javascript" type="text/javascript" src="'._JOOMLA_JL_LIVE_SITE.'/includes/js/calendar/calendar_mini.js"></script>
  <!-- import the language module -->
  <script language="javascript" type="text/javascript" src="'._JOOMLA_JL_LIVE_SITE.'/includes/js/calendar/lang/calendar-en.js"></script>
	
  <!-- import extension specific module -->
  <script language="javascript" type="text/javascript">
  
  function update_points( partid, gameId )
  {
    var f = document.adminForm;var i=1;
    var scores = document.getElementsByName("matchpart"+partid+"_result_split"+gameId+"[]");
    var goals = 0;
    var behinds = 0;
	  for ( i=0; i < scores.length; i++)
	  {
	    if ( scores[i].value.test("^[0-9]+\.[0-9]$") ) 
	    {
	      var res=scores[i].value.split(".");
	      goals += parseInt(res[0]);
	      behinds += parseInt(res[1]);
	    }
	  }
	  var points = document.getElementsByName("matchpart"+partid+"_result"+gameId);
	  points[0].value = goals * 6 + behinds;
  }
  </script>
  ' );
?>
<div style="overflow:auto;">
<table class="contentpaneopen" style="width:auto;">
	<tr>
		<td class="contentheading">
    <?php 
    if ($current_round>0) {
      HTML_joomleague::show_matchdays_title(_JL_RESULTS, $rounds[$current_round-1]->id, $results_config);
	    if (defined('JL_SHOW_EDIT_ICON')) HTML_joomleague::show_edit_icon($current_round, $jl_func);
    }
    else {
      if (defined('JL_SHOW_EDIT_ICON')) {
        echo _JL_RESULTS_EDIT_ALL;
        HTML_joomleague::show_edit_icon($current_round, $jl_func);
      }
    }
    ?>
    </td>
  </tr>
  <tr>
    <td>
<?php
	$mdd = HTML_joomleague::init_edit_events();
?>
    </td>
  </tr>
  <tr>
    <td>
<form name="adminForm" id="adminForm" method="post" action="<?php echo JL_COMPONENT_LINK.JL_ITEMID_SUFFIX;?>">
<input type="hidden" name="p" value="<?php echo $joomleague->id;?>" />
<input type="hidden" name="r" value="<?php echo $current_round;?>" />
<input type="hidden" name="sel_r" value="<?php echo $current_round;?>" />
<input type="hidden" name="option" value="com_joomleague" />
<input type="hidden" name="func" value="saveResults" />
<input type="hidden" name="oldfunc" value="<?php echo $jl_func;?>" />
<input type="hidden" name="boxchecked" id="boxchecked" value="0" />
<input type="hidden" name="checkmycontainers" id="checkmycontainers" value="0" />
<?php
if ($joomleague->current_round_auto==0) {
  echo '<table><tr><td style="text-align:right"><b>'._JL_RESULTS_SET_MATCHDAY.'</b>'
  .$joomleague->roundsdropdown($current_round, count($rounds)).'</td></tr></table>';
  }
?>

<br />

<!-- Main START -->
<table border="0" cellpadding="3" cellspacing="0">
<?php
if (count($matches) > 0) {
  $k=0;
	if ((isset($overall_config['results_below'])&&$overall_config['results_below']==1) ||
	   (isset($overall_config['edit_results_below'])&&$overall_config['edit_results_below']==1)) {
	    $results_config['results_below']=1;
	}
	else $results_config['show_match_number']=1;
  $results_config['show_events']=0;
  $results_config['show_division']=0;
  $results_config['show_playground']=1;
  $results_config['show_logo_small']=0;
  

  HTML_joomleague::resultsColspan($results_config, 1);

  $lastdatum = '';
  $playing_teams = array();
  $i=0;
  $favteams = array();
  
  foreach($matches as $match) {
    $results_config['class'] = ($k == 0)? $results_config['style_class1'] : $results_config['style_class2'];
    
    if (     defined('JL_EDIT_MODE') 
          && ( $match->checked_out==0 || $match->checked_out == $my->id )
          && (     defined('JL_USER_IS_PROJECT_ADMIN') 
                || defined('JL_USER_IS_PROJECT_EDITOR')
                || defined('JL_USER_IS_BIG_BOSS')
                || $joomleague->isMatchAdmin($match->match_id, $my->id) )
             )
    {
      HTML_joomleague::newDayHeading ($match->match_date, $olddate, $results_config);
      
      $hometeam = $resultTeams[$match->matchpart1];
      $guestteam = $resultTeams[$match->matchpart2];
      
      $mdd[] = mosHTML::makeOption( _JOOMLA_JL_LIVE_SITE."/".JL_COMPONENT_LINK_NOHTML."&amp;func=editEvents&amp;p=".$joomleague->id
		 			   ."&amp;mid=".$match->match_id, $hometeam->$results_config['names']." - ".$guestteam->$results_config['names'] );

      editable_row($match, $resultTeams, $i, $results_config);

?>
    <tr style="margin:0;padding:0" class="<?php echo $results_config['class'];?>">
      	<td colspan="<?php echo $results_config['colspan'];?>" style="margin:0;padding:0">
      	  <div id="details<?php echo $match->match_id;?>" style="display:none;margin:0;padding:0"></div>
      	</td>
    </tr>
<?php
    }
    $k = 1 - $k;
    $i++;
  }
}
?>
          
</table>
<div align="center">
  <input type="submit" class="button" name="saveResults" value="<?php echo _JL_SAVE;?>" onclick="$('checkmycontainers').value=0;" />
</div>
<!-- Main END -->
<input type="hidden" class="button" name="save_data" value="1" />
</form>
     </td>
   </tr>
 </table>
</div>
<?php
HTML_joomleague::close_edit_events($mdd);

/*
if ($results_config['show_dnp_teams'] == 1) {
  echo '<div style="text-align:center">';
  HTML_joomleague::show_not_playing_teams($playing_teams, $teams_in_league, $results_config);
  echo '</div>';
}
*/
?>

<!-- Matchdays navigation Output  -->
<?php 
if ( isset($pageNav) ) 
{ 
  ?>
  <table width="96%" align="center" border="0" cellpadding="3" cellspacing="0">
    <tr>
      <td align="center">
        <?php 
        echo $pageNav;
        ?>
      </td>
    <tr>
      <td align="center">
        <?php 
        echo '<a href="'.sefRelToAbs(JL_COMPONENT_LINK.'&amp;func=editResults&amp;p='.$joomleague->id).'">';
        echo _JL_RESULTS_EDIT_ALL_ROUNDS;
        echo '</a>';
        ?>        
      </td>
    </tr>
  </table>
  <?php 
}
?>
<!-- Matchdays navigation Output END --> 

<!-- Print Button START -->
<?php if ($results_config['show_back_button'] > 0 OR $overall_config['show_back_button'] > 0) {;?>
<?php if ($results_config['show_back_button'] == "1" OR $overall_config['show_back_button'] == "1") echo '<div align="left">'; else echo '<div align="right">';?>
<?php
$params = & new mosParameters( '' );
$params->def( 'back_button', true );
$params->def( 'popup', false );
mosHTML::BackButton( $params );
?></div> 
<?php };?>
<!-- Print Button END -->
